##Making Spatial Map
##setting work directory
setwd("~/Desktop/Data science Assignments")
##Loading of  require packages
#install.packages("tmap")
#install.packages("leaflet")
#install.packages("sf")
#install.packages("rgdal")
#install.packages("raster")
#install.packages("rgeos")
#install.packages("ggplot2")
#install.packages("htmlwidgets")
#install.packages("grid")
#install.packages("gifski")
library(tmap)
library(leaflet)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-29, (SVN revision 1165M)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.0, released 2021/11/04
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 8.1.1, September 1st, 2021, [PJ_VERSION: 811]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/proj
library(raster)
library(rgeos)
## rgeos version: 0.5-9, (SVN revision 684)
##  GEOS runtime version: 3.8.1-CAPI-1.13.3 
##  Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
##  Linking to sp version: 1.4-6 
##  Polygon checking: TRUE
library(ggplot2)
library(htmlwidgets)
library(grid)
library(gifski)
##loading shape file 
state<-st_read("/Users/carlkwamesarfo/Desktop/Data science Assignments")
## Multiple layers are present in data source /Users/carlkwamesarfo/Desktop/Data science Assignments, reading layer `tl_2019_us_state'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
## Reading layer `tl_2019_us_state' from data source 
##   `/Users/carlkwamesarfo/Desktop/Data science Assignments' using driver `ESRI Shapefile'
## Simple feature collection with 56 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.2311 ymin: -14.60181 xmax: 179.8597 ymax: 71.43979
## Geodetic CRS:  NAD83
IN = state[state$STUSPS== "IN",]
##Loading watershed data
watershed<-st_read("/Users/carlkwamesarfo/Desktop/Data science Assignments")
## Multiple layers are present in data source /Users/carlkwamesarfo/Desktop/Data science Assignments, reading layer `tl_2019_us_state'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
## Reading layer `tl_2019_us_state' from data source 
##   `/Users/carlkwamesarfo/Desktop/Data science Assignments' using driver `ESRI Shapefile'
## Simple feature collection with 56 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.2311 ymin: -14.60181 xmax: 179.8597 ymax: 71.43979
## Geodetic CRS:  NAD83
summary(watershed)
##     REGION            DIVISION           STATEFP            STATENS         
##  Length:56          Length:56          Length:56          Length:56         
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##     GEOID              STUSPS              NAME               LSAD          
##  Length:56          Length:56          Length:56          Length:56         
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##     MTFCC             FUNCSTAT             ALAND               AWATER         
##  Length:56          Length:56          Min.   :1.583e+08   Min.   :1.869e+07  
##  Class :character   Class :character   1st Qu.:2.483e+10   1st Qu.:1.503e+09  
##  Mode  :character   Mode  :character   Median :1.286e+11   Median :3.706e+09  
##                                        Mean   :1.636e+11   Mean   :1.245e+10  
##                                        3rd Qu.:2.008e+11   3rd Qu.:8.964e+09  
##                                        Max.   :1.479e+12   Max.   :2.454e+11  
##    INTPTLAT           INTPTLON                  geometry 
##  Length:56          Length:56          MULTIPOLYGON :56  
##  Class :character   Class :character   epsg:4269    : 0  
##  Mode  :character   Mode  :character   +proj=long...: 0  
##                                                          
##                                                          
## 
names(watershed)
##  [1] "REGION"   "DIVISION" "STATEFP"  "STATENS"  "GEOID"    "STUSPS"  
##  [7] "NAME"     "LSAD"     "MTFCC"    "FUNCSTAT" "ALAND"    "AWATER"  
## [13] "INTPTLAT" "INTPTLON" "geometry"
head(watershed)
## Simple feature collection with 6 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -97.23909 ymin: 24.39631 xmax: -71.08857 ymax: 49.38448
## Geodetic CRS:  NAD83
##   REGION DIVISION STATEFP  STATENS GEOID STUSPS          NAME LSAD MTFCC
## 1      3        5      54 01779805    54     WV West Virginia   00 G4000
## 2      3        5      12 00294478    12     FL       Florida   00 G4000
## 3      2        3      17 01779784    17     IL      Illinois   00 G4000
## 4      2        4      27 00662849    27     MN     Minnesota   00 G4000
## 5      3        5      24 01714934    24     MD      Maryland   00 G4000
## 6      1        1      44 01219835    44     RI  Rhode Island   00 G4000
##   FUNCSTAT        ALAND      AWATER    INTPTLAT     INTPTLON
## 1        A  62266231560   489271086 +38.6472854 -080.6183274
## 2        A 138947364717 31362872853 +28.4574302 -082.4091477
## 3        A 143779863817  6215723896 +40.1028754 -089.1526108
## 4        A 206230065476 18942261495 +46.3159573 -094.1996043
## 5        A  25151726296  6979340970 +38.9466584 -076.6744939
## 6        A   2677787140  1323663210 +41.5974187 -071.5272723
##                         geometry
## 1 MULTIPOLYGON (((-81.74725 3...
## 2 MULTIPOLYGON (((-86.38865 3...
## 3 MULTIPOLYGON (((-91.18529 4...
## 4 MULTIPOLYGON (((-96.78438 4...
## 5 MULTIPOLYGON (((-77.45881 3...
## 6 MULTIPOLYGON (((-71.7897 41...
# map objects creation
map_watershed<-tm_shape(watershed) +
  tm_polygons(col="REGION")
map_watershed

class(map_watershed)
## [1] "tmap"
## adding borders to plot
mapWatershed <- tm_shape(state) +
  tm_fill(col = "REGION") + tm_borders()

mapWatershed

##Making animation of object
mapZH <- tm_shape(IN) +
  tm_borders()+tm_shape(watershed) +
  tm_polygons()+
  tm_facets(along = "REGION", free.coords = FALSE)
tmap_animation(mapZH, filename = "IN_Watershed.gif", delay = 25)
## Creating frames
## ================================
## ================
## ================
## ================
## 
## Creating animation
## Animation saved to /Users/carlkwamesarfo/Desktop/IN_Watershed.gif
tmap_EFH <-tmap_leaflet(mapZH, mode = "view", show = FALSE) %>% setView(1249033,2682288,zoom=12)%>% fitBounds(-72, 40, -70, 43)%>% clearBounds() 
## Warning: along not supported in view mode
tmap_EFH
saveWidget(tmap_EFH, file="tmap_EFH_anz.html")

SPATIAL MAP

map_watershed + 
  tm_shape(watershed)+ 
  tmap_EFH + 
  tm_fill("red", alpha = 0.4)+ 
  tm_scale_bar(breaks = c(0, 100, 200), text.size = 0.4)+ 
  tm_layout(bg.color="lightblue", 
            title = "Indiana Watershed", 
            legend.width = 2, 
            inner.margins=c(.20,.30, .36, .36), 
            #earth.boundary = TRUE, 
            space.color="grey90") + 
  tm_legend(position = c("left", "top"), 
            frame = FALSE, 
            bg.color="lightblue") + 
  tm_compass(position = c(.45, 0.65), color.light = "grey90", type = "4star") 
## Scale bar set for latitude km and will be different at the top and bottom of the map.